const { getStores, getHotStores, getStoreById, getStoresByLocation } = require('../models/Store');

const getAllStores = async (req, res) => {
    try {
        const { type, keyword, page = 1, size = 10 } = req.query;
        const stores = await getStores({ type, keyword, page, size });
        res.json({
            code: 200,
            data: stores
        });
    } catch (error) {
        console.error(error);
        res.status(500).json({
            code: 500,
            message: '获取店铺列表失败'
        });
    }
};

const getHotRecommends = async (req, res) => {
    try {
        const hotStores = await getHotStores();
        res.json({
            code: 200,
            data: hotStores
        });
    } catch (error) {
        console.error(error);
        res.status(500).json({
            code: 500,
            message: '获取热点推荐失败'
        });
    }
};

const getStoreDetails = async (req, res) => {
    try {
        const { storeId } = req.params;
        console.log('[CONTROLLER] 获取店铺详情:', { storeId });
        
        const store = await getStoreById(storeId);
        if (!store) {
            console.log('[CONTROLLER] 店铺不存在:', { storeId });
            return res.status(404).json({
                code: 404,
                message: '店铺不存在'
            });
        }
        
        console.log('[CONTROLLER] 店铺详情返回:', { 
            id: store.id,
            name: store.name,
            hasLatitude: !!store.latitude,
            hasLongitude: !!store.longitude,
            latitude: store.latitude,
            longitude: store.longitude
        });
        
        // 直接返回店铺数据，不要包装在data字段中
        res.json(store);
    } catch (error) {
        console.error('[CONTROLLER] 获取店铺详情失败:', error);
        res.status(500).json({
            code: 500,
            message: '获取店铺详情失败'
        });
    }
};

const getNearbyStores = async (req, res) => {
    try {
        const { 
            latitude, 
            longitude, 
            distance = 5000,
            type = 'all',
            sort = 'distance',
            asc = 0,
            keyword = '',
            page = 1,
            pageSize = 10
        } = req.query;

        console.log('[CONTROLLER] 获取附近店铺:', { 
            latitude, longitude, distance, type, sort, asc, keyword, page, pageSize 
        });

        if (!latitude || !longitude) {
            return res.status(400).json({
                code: 400,
                message: '缺少经纬度参数'
            });
        }

        // 转换参数类型
        const options = {
            latitude: parseFloat(latitude),
            longitude: parseFloat(longitude),
            radius: parseFloat(distance),
            type,
            sort,
            asc: parseInt(asc),
            keyword,
            page: parseInt(page),
            pageSize: parseInt(pageSize)
        };

        const result = await getStoresByLocation(options);
        
        res.json({
            code: 200,
            data: result.list,
            pagination: result.pagination
        });
    } catch (error) {
        console.error('[CONTROLLER] 获取附近店铺失败:', error);
        res.status(500).json({
            code: 500,
            message: '获取附近店铺失败'
        });
    }
};

module.exports = { getAllStores, getHotRecommends, getStoreDetails, getNearbyStores };